import { useTranslation } from 'react-i18next';
import { translateDataField, translateDataObject } from '../utils/i18nDataTranslator';

/**
 * Hook用于翻译后端数据
 * 当语言切换时，会自动重新计算翻译
 */
export const useTranslateData = (namespace: string = 'grade') => {
  const { t } = useTranslation(namespace);

  return {
    translateField: (value: string | number | undefined | null, fieldType: string) =>
      translateDataField(value, fieldType, t, namespace),
    translateObject: <T extends Record<string, unknown>>(
      data: T | T[],
      fieldsToTranslate: Record<string, string>
    ) => translateDataObject(data, fieldsToTranslate, t, namespace)
  };
};
